﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ras2Logic;

namespace Ras2Logic
{
    /// <summary>
    /// Klasinn HitListController
    /// </summary>
    public class HitListController
    {
        #region Member breytur

        /// <summary>
        /// Memberbreyta sem inniheldur tengingu við databaseinn.
        /// </summary>
        private Ras2DBDataContext m_context = new Ras2DBDataContext();

        #endregion

        #region Smiðir

        #endregion

        #region Get / Set föll

        #endregion

        #region Private föll

        #endregion

        #region Public föll

        /// <summary>
        /// Fall sem sækir alla vinsældarlista úr gagnagrunni
        /// </summary>
        /// <returns>Skilar safni af vinsældarlistum</returns>
        public IEnumerable<HitList> GetAllHitlists()
        {
            //Þetta skilar öllu úr HitLists töfluni.
            var result = from hitlist in m_context.HitLists
                         select hitlist;
            return result;
        }

        /// <summary>
        /// Fall sem sækir ákveðinn vinsældarlista eftir ID
        /// </summary>
        /// <param name="nID">Tekur inn ID á vinsældarlista</param>
        /// <returns>Skilar vinsældarlista af tegund HitList</returns>
        public HitList GetHitListById(int nID)
        {
            return (from a in m_context.HitLists
                    where a.ID == nID
                    select a).SingleOrDefault();
        }

        /// <summary>
        /// Fall sem sækir öll lög inn í vinsældarlista.
        /// </summary>
        /// <param name="nID">Tekur inn ID listanns</param>
        /// <returns>Skilar safni af lögum sem eru á viðkomandi lista</returns>
        public IEnumerable<vGetAllHitListEntry> GetAllHitListEntries(int nID)
        {
            var result = from a in m_context.vGetAllHitListEntries
                         where a.HitListID == nID
                         orderby a.Position ascending
                         select a;
            return result;
        }

        /// <summary>
        /// Fall sem uppfærir vinsældarlista.
        /// </summary>
        /// <param name="s">Tekur inn vinsældarlista af tegund HitList</param>
        public void UpdateHitList(HitList s)
        {
            HitList original = (from a in m_context.HitLists
                                where a.ID == s.ID
                                select a).SingleOrDefault();

            if (original != null)
            {
                original.ID = s.ID;
                original.Name = s.Name;

                m_context.SubmitChanges();
            }
        }

        /// <summary>
        /// Fall sem bætir inn nýjum vinsældarlista
        /// </summary>
        /// <param name="s">Tekur inn vinsældarlista af tegund HitList</param>
        public void AddHitList(HitList s)
        {
            m_context.HitLists.InsertOnSubmit(s);
            m_context.SubmitChanges();
        }

        #endregion
    }
}
